import os
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from taggit.managers import TaggableManager
from mediabox.lira.utils import Transliterator

CONTENT_TYPES = (
    ("video", _("Video")),
    ("audio", _("Audio")),
    ("picture", _("Picture")),
    ("document", _("Document")),
    ("other", _("Others")),
)

def get_path(instance, filename):
    filepath = os.path.join('/', instance.item_type, Transliterator.cyr2lat(filename))

    if not os.path.exists(filepath):
        os.mkdir(filepath)

    return filepath

class File(models.Model):
    filename = models.CharField(max_length=255)
    uri = models.CharField(max_length=255)
    filemime = models.CharField(max_length=100)
    filesize = models.IntegerField(max_length=40)
    status = models.CharField(max_length=12)
    timestamp = models.CharField(max_length=20)

    def __unicode__(self):
        return self.filename

    class Meta:
        app_label = "lira"

class Program(models.Model):
    title = models.CharField(max_length=200, blank=False)
    description = models.TextField()

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ["title"]
        app_label = "lira"

class Topic(models.Model):
    title = models.CharField(max_length=200, blank=False)
    description = models.TextField()

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ["title"]
        app_label = "lira"

class Region(models.Model):
    region_name = models.CharField(max_length=40)

    def __unicode__(self):
        return self.region_name

    class Meta:
        ordering = ["-region_name"]
        app_label = "lira"

class Document(models.Model):
    title = models.CharField(max_length=200, db_index=True)
    description = models.TextField()
    program = models.ForeignKey(to=Program)
    topic = models.ForeignKey(to=Topic)
    region = models.ForeignKey(to=Region)
    myfile = models.ForeignKey(to=File, blank=True, null=True)
    item_type = models.CharField(max_length=10, choices=CONTENT_TYPES, default='others')
    created = models.DateField(blank=True, null=True)
    changed = models.DateField(blank=True, null=True)
    tags = TaggableManager(blank=True)
    owner = models.ForeignKey(to=User, blank=True, null=True)
    announce = models.BooleanField(default=False)
    processed = models.BooleanField(default=False)

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ["-created"]
        app_label = "lira"
